﻿#include "Fighting.h"
#include"WeaponBank.h"
#include"Sprite.h"
#include"Enemy.h"
#include"Collider.h"
#include<Windows.h>
#include<qdebug.h>
#include<QPushButton>
#include"SupplyBank.h"
#include"Game.h"
#include"Button.h"
#include"ProtectionCover.h"
#include"Message.h"
#include<thread>
Fighting::Fighting(QSize size, QObject* parent)
    :m_Size(size) 
{ 
    this->m_parent = parent;
    init();
}
Fighting::~Fighting()
{
    //背景
    if (m_back != nullptr)
    {
       delete m_back;
       m_back = nullptr;
    }
    //暂停继续按钮
    if (m_pauseButton != nullptr)
    {
        delete m_pauseButton;
        m_pauseButton = nullptr;
    }
    //玩家
    if (m_Player != nullptr)
    {
        delete m_Player;
        m_Player = nullptr;
    }

    //信息显示
    if (m_Message != nullptr)
    {
        delete m_Message;
        m_Message = nullptr;
    }
    //补给库
    if (m_SupplyBank != nullptr)
    {
        delete m_SupplyBank;
        m_SupplyBank = nullptr;
    }
    //敌机正营
    if (m_enemy != nullptr)
    {
        delete m_enemy;
        m_enemy = nullptr;
    }

    //碰撞器
    if (m_Collider != nullptr)
    {
        delete m_Collider;
        m_Collider = nullptr;
    }
}
void Fighting::init()
{
    //创建背景
    if (m_back == nullptr)
    {
        m_back = new Background();
    }
    //暂停继续按钮
    if (m_pauseButton == nullptr)
    {
        //qDebug() << parent;
        m_pauseButton = new Button(Game::res().getPixmaps("暂停按钮"),(QWidget*)m_parent);
        m_pauseButton->setFocusPolicy(Qt::FocusPolicy::NoFocus);
       // m_pauseButton->clearFocus();
        QObject::connect(m_pauseButton, &QPushButton::clicked, [this]() {
            if (m_pause==false)
            {
                m_pauseButton->setPixmaps(Game::res().getPixmaps("继续按钮"));
                auto Protection = m_Player->getProtection();
                if(Protection!=nullptr)
                    Protection->setPause(true);
                m_pause = true;
                m_enemy->setPause(true);
                m_Player->setPause(true);
                //m_PlayerWeaponBank->setPause(true);
                m_SupplyBank->setPause(true);
            }
            else
            {
                m_pauseButton->setPixmaps(Game::res().getPixmaps("暂停按钮"));
                auto Protection = m_Player->getProtection();
                if (Protection != nullptr)
                    Protection->setPause(false);
                m_pause = false;
                m_enemy->setPause(false);
                m_Player->setPause(false);
                //m_PlayerWeaponBank->setPause(false);
                m_SupplyBank->setPause(false);
            }
            });
        m_pauseButton->move(400, 0);
        m_pauseButton->setFixedSize(50,50);
        m_pauseButton->show();
    }
    //创建玩家
    if (m_Player == nullptr)
    {
        m_Player = new Sprite(m_Size.width() * 0.4, m_Size.height() * 0.7, m_Size,m_parent);
       
       /* m_Player->setWindowSize();
        m_Player->creatorWeaponBank();*/
        m_Player->startBlood(10, Plane::Direction::up);//开启血条
        m_Player->setProtectTime(5000);
        m_Player->startProtect();
    }
   
    //创建信息显示
    if (m_Message == nullptr)
    {
        m_Message = new Message(m_Player);
       
        //m_playBullet->setCollider(QRect(QPoint(0, 0), m_Size));
        //m_PlayerWeaponBank->setWindowSize(m_Size);
        //m_PlayerWeaponBank->setProperty(QVector2D(0, -1), 500);
        //m_PlayerWeaponBank->setBulletTimer(200);
    }

    //补给库
    if (m_SupplyBank == nullptr)
    {
        m_SupplyBank = new SupplyBank(3);
        m_SupplyBank->setWindow(QRect(0, 0, m_Size.width(), m_Size.height()));
       // m_SupplyBank->bindPlane(m_Player);
       // m_SupplyBank->setTimerSupply(8000,15000,Supply::bullet);
        m_SupplyBank->startAuto();
    }

    //创建敌机正营
    if (m_enemy == nullptr)
    {
        m_enemy = new Enemy();
        m_enemy->setWindow(QRect(0, 0, m_Size.width(), m_Size.height()));
        m_enemy->startBlood(10, Plane::Direction::down);
        //m_enemy->setPlaneType(EnemyPlaneProperty::Type::big);
        m_enemy->startAuto();
    }
    
    //创建碰撞器
    if (m_Collider == nullptr)
    {
        m_Collider = new Collider();
        //m_collider->setPlayerBulletBank(m_Player->getPtrplayWeaponBank());
        m_Collider->setEnemy(m_enemy);
        m_Collider->setPlayer(m_Player);
        m_Collider->setSupplyBank(m_SupplyBank);
        m_Collider->startTrackingPlayers(300);
    }
    setPlayerHp(3, 3);
    setType(Type::fighting);
}

void Fighting::update()
{
    /*if (m_pause)
        return;*/
    //std::thread t1([this]() {keyMoveby(); });
   /* std::thread t2([this]() {if (m_back != nullptr)
        m_back->update(); });*/
    //std::thread t3([this]() {if (m_Player != nullptr)
    //    m_Player->update(); });
    //std::thread t4([this]() {if (m_Message != nullptr)
    //    m_Message->update(); });
    //std::thread t5([this]() {if (m_enemy != nullptr)
    //    m_enemy->update(); });
    //std::thread t6([this]() {if (m_SupplyBank != nullptr)
    //    m_SupplyBank->update(); });
    //std::thread t7([this]() { m_collider->update(); });
  
    
    
    keyMoveby();
    if (m_back != nullptr)
        m_back->update();
    if (m_Player != nullptr)
        m_Player->update();
    if (m_Message != nullptr)
        m_Message->update();
    if (m_enemy != nullptr)
        m_enemy->update();
    if (m_SupplyBank != nullptr)
        m_SupplyBank->update();
    m_Collider->update();
    //t1.join();
   // t2.join();
    //t3.join();

    //t4.join();
    //t5.join();
    //t6.join();
    //t7.join();
}

void Fighting::draw(QPainter* painter)
{
    if(m_back!=nullptr)
        m_back->draw(painter);
    if (m_enemy != nullptr)
        m_enemy->draw(painter);
    if (m_Player != nullptr)
        m_Player->draw(painter);
    if (m_Message != nullptr)
        m_Message->draw(painter);
    if (m_SupplyBank != nullptr)
        m_SupplyBank->draw(painter);
       
}

void Fighting::keyMoveby()
{
    if (m_Player==nullptr||!m_Player->TimerTrigger())
        return;
    if (GetAsyncKeyState(VK_UP))//上
    {
        m_Player->moveBy(0, -1 * m_KeySteplength);
    }
    if (GetAsyncKeyState(VK_DOWN))//下
    {
        m_Player->moveBy(0, 1 * m_KeySteplength);
    }
    if (GetAsyncKeyState(VK_LEFT))//左
    {
        m_Player->moveBy(-1 * m_KeySteplength, 0);
    }
    if (GetAsyncKeyState(VK_RIGHT))//右
    {
        m_Player->moveBy(1 * m_KeySteplength, 0);
    }
    if (GetAsyncKeyState(VK_SPACE))//空格
    {
        int x = m_Player->getPos().x() + m_Player->getSize().width() / 2;
        int y = m_Player->getPos().y();
       // qDebug() << "发射";
        m_Player->getWeaponBank()->addBullet(x, y);
    }
    if (GetAsyncKeyState(VK_CONTROL)&& m_Player->getBombNum()>0)//Ctrl键
    {
        int x = m_Player->getPos().x() + m_Player->getSize().width() / 2;
        int y = m_Player->getPos().y();
         //qDebug() << "发射";
        m_Player->getWeaponBank()->addGuidedMissile(x-24, y,*m_Player);
        //m_Player->reduceBombNum();
    }
}

void Fighting::setKeySteplength(const int Steplength)
{
    m_KeySteplength = Steplength;
}

Background* Fighting::getPtrBackground()
{
    return m_back;
}

void Fighting::setPlayerHp(const int Hp, const int MaxHp)
{
    if (m_Player == nullptr)
        return;
    m_Player->setHp(Hp);
    m_Player->setMaxHp(MaxHp);
}

WeaponBank* Fighting::getPtrplayBullet()
{
    if (m_Player != nullptr)
    {
        return m_Player->getWeaponBank();
    }
    return nullptr;
}

Enemy* Fighting::getPtrEnemy()
{
    return m_enemy;
}

Collider* Fighting::getPtrCollider()
{
    return m_Collider;
}

Sprite* Fighting::getPtrPlayer()
{
    /*if (m_Player == nullptr)
        return Sprite();*/
    return m_Player;
}
